<?php



namespace XtwsBack\Controller;

use Think\Controller;



class CommunityController extends Controller{

    public function Index(){

        $area=M('area');

        $community=M('shop_community');
		$where="";
		if($_GET['keyword']!=''){
			$where="title like '%".$_GET['keyword']."%'";
			$this->assign('keyword',$_GET['keyword']);// 赋值分页输出
		}
        $count = $community->count();// 查询满足要求的总记录数

        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show = $Page->show();// 分页显示输出

        $community_arr =$community->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach($community_arr as $k => $v){

            $areatitle = $area->where('id ='.$v['a_id_1'])->find();

            $community_arr[$k]['a_id_1'] = $areatitle['name'];

            $areatitle = $area->where('id ='.$v['a_id_2'])->find();

            $community_arr[$k]['a_id_2'] = $areatitle['name'];

            $areatitle = $area->where('id ='.$v['a_id_3'])->find();

            $community_arr[$k]['a_id_3'] = $areatitle['name'];

        }

        $this->assign('list',$community_arr);// 赋值数据集

        $this->assign('page',$show);// 赋值分页输出

        $this->display();

    }//end fun index



    public function Addcommunity(){

        $area=D('area');

        $community=M('shop_community');

        $getfirst=$area->where("`re_id` =0")->select();

        $postcommunity = I("post.");

        if ($postcommunity){
            $comm = $community->where(array('title'=>$postcommunity['title'],'a_id_1'=>$postcommunity['a_id_1'],'a_id_2'=>$postcommunity['a_id_2'],'a_id_3'=>$postcommunity['a_id_3']))->find();
            if($comm!=''){
                $this->error('该小区已经存在','index.html');
            }
//            die;
            $addcommunity = $community->add($postcommunity);

            if ($addcommunity){

                $this->success('添加成功','index.html');
                return;

            }else{

                $this->error('添加失败','index.html');

            }

        }

        $this->assign('areali',$getfirst);

        $this->display();

    }//end fun add



    public function Savecommunity(){

        $area=D('area');

        $community=M('shop_community');



        $community_id = I("get.id")?I("get.id"):I("post.id");



        $getfirst=$area->where("`re_id` =0")->select();



        $getcommunity = $community->where('id ='.$community_id)->find();



//        var_dump($getcommunity);die;

        $areatitle1 = $area->where('id ='.$getcommunity['a_id_1'])->find();

        $getcommunity['a_id_1'] = $areatitle1['name'];

        $areatitle2 = $area->where('id ='.$getcommunity['a_id_2'])->find();

        $getcommunity['a_id_2'] = $areatitle2['name'];

        $areatitle3 = $area->where('id ='.$getcommunity['a_id_3'])->find();

        $getcommunity['a_id_3'] = $areatitle3['name'];



        $postcommunity = I("post.");

        if ($postcommunity){
            foreach($postcommunity as $k =>$v){

                if ($v == ''){

                    unset($postcommunity[$k]);

                }elseif($v == '0'){

                    unset($postcommunity[$k]);

                }



                foreach ($getcommunity as $key => $value){

                    if ($postcommunity[$key] == $value){

                        unset($postcommunity[$key]);

                    }

                }

            }

            if ($postcommunity){

                $savecommunity = $community->where('id ='.$community_id)->save($postcommunity);

                if ($savecommunity){

                    $this->success('修改成功');
                    return;

                }else{

                    $this->error('修改失败');

                }

            }else{

                $this->error("无修改","index.html");

            }



        }

        $this->assign('community',$getcommunity);

        $this->assign('areali',$getfirst);

        $this->display();

    }

}//end class